/**
 * Created by Administrator on 2016/3/16 0016.
 */
angular.module('app.ad', ['ui.router']).config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home1', {
            'abstract': true, url: '', views: {
                '': {
                    templateUrl: "view/home.html"
                }
            }
        })
        .state('home1.first', {
            'abstract': true,
            url: '/home1',
            views: {
                'topbar': {
                    templateUrl: 'view/topbar.html',
                    controller: 'TopbarController'
                }, 'main': {
                    templateUrl: 'view/home/ad/sidebar.html',
                    controller: 'HomebaseController'
                }
            }
        }).state('home1.first.ad', {
            url: '/ad',
            templateUrl: 'view/home/ad/ad.html',
            controller: 'AdvertiseController'
        })

        .state('home1.first.addetail', {
            url: '/admodify/:id',
            templateUrl: 'view/home/ad/ad.detail.html',
            controller: 'AdvertiseDetailController'
        }).state('home1.first.adbind', {
            url: '/addetail/:id',
            templateUrl: 'view/home/ad/ad.bind.html',
            resolve: {
                relation_list : ['$stateParams','RelationService', function($stateParams, RelationService){
                    return RelationService.getbyaid($stateParams.id).$promise;
                }]
            },
            controller: 'AdvertiseBindController'
        }).state('home1.first.adadd', {
            url: '/adadd/',
            templateUrl: 'view/home/ad/ad.add.html',
            controller: 'AdvertiseAddController',
        }).state('home1.first.adadd1', {
            url: '/adadd1/:id',
            templateUrl: 'view/home/ad/ad.add1.html',
            controller: 'AdvertiseAdd1Controller'
        })

}])

    .controller("AdvertiseController", ['$scope', 'AdvService',  function ($scope, AdvService) {
        AdvService.getbyall().$promise.then(function (data) {
            $scope.advs = data;
        });
        $scope.currentPage = 1;
        $scope.pageSize = 8;

    }])
    .controller("AdvertiseDetailController", ['$scope', '$stateParams', '$state', 'AdvService', function ($scope, $stateParams, $state, AdvService) {
        AdvService.getbyid($stateParams.id).$promise.then(function(data){
            $scope.ad=data;
        })
    }])
    .controller("AdvertiseBindController", ['$scope', '$stateParams','relation_list', 'RelationService','MarketService', function ($scope, $stateParams,relation_list, RelationService, MarketService) {
        //
        $scope.aid=$stateParams.id;
        $scope.relation_list=relation_list;
        $scope.markets=new Array();
        for(var i=0;i<$scope.relation_list.length;i++){
            MarketService.getbyid($scope.relation_list[i].mId).$promise.then(function(data){
                $scope.markets.push(data);
            })
        }
        //
        $scope.markets_outdate=new Array();
        RelationService.getoutdate().$promise.then(function(data){
            $scope.relation_outdatelist=data;
            for(var i=0;i<$scope.relation_outdatelist.length;i++){
                MarketService.getbyid($scope.relation_outdatelist[i].mId).$promise.then(function(data){
                    $scope.markets_outdate.push(data);
                })
            }
        })

        //unbind
        $scope.selection_unbind=new Array();
        $scope.prepare_canceldata=function(market){
            var  ss=true;
            if($scope.selection_unbind.length==0){
                $scope.selection_unbind.push(market);
            }else{
                for(var i=0;i<$scope.selection_unbind.length;i++){
                    if($scope.selection_unbind[i].mId==market.mId){
                        $scope.selection_unbind.splice(i,1);
                        ss=false;
                    }
                }
                if(ss){
                    $scope.selection_unbind.push(market);
                }
            }
        }
        $scope.prepare_canceldata1=function(){
            if($scope.master){
                $scope.selection_unbind=[];
                for(var i=0;i<$scope.markets.length;i++){
                    $scope.selection_unbind.push($scope.markets[i]);
                }
            }else{
                $scope.selection_unbind=[];
            }
        }
        $scope.submit_unbind = function () {
            console.log($scope.selection_unbind[0]);
            if($scope.selection_unbind.length==0){
                alert("未选择商超！");
            }else{
                for(var i=0;i<$scope.selection_unbind.length;i++){
                    var j=0;
                    RelationService.deletebyid($scope.selection_unbind[i].mId,$scope.aid).$promise.then(function(data){
                        j++;
                        if(j==$scope.selection_unbind.length){
                            alert("解除绑定成功！");
                            window.location.reload();
                        }
                    })
                }
            }
        };

       //bind
        $scope.selection_bind=new Array();
        $scope.prepare_data=function(market){
            var  ss=true;
            if($scope.selection_bind.length==0){
                $scope.selection_bind.push(market);
            }else{
                for(var i=0;i<$scope.selection_bind.length;i++){
                    if($scope.selection_bind[i].mId==market.mId){
                        $scope.selection_bind.splice(i,1);
                        ss=false;
                    }
                }
                if(ss){
                    $scope.selection_bind.push(market);
                }
            }
            //console.log($scope.selection_bind);
        }
        $scope.prepare_data1=function(){
            if($scope.master1){
                $scope.selection_bind=[];
                for(var i=0;i<$scope.markets_outdate.length;i++){
                    $scope.selection_bind.push($scope.markets_outdate[i]);
                }
            }else{
                $scope.selection_bind=[];
            }
            //console.log($scope.selection_bind);
        }
        $('#datetimepicker').datetimepicker({
            format: "yyyy-MM-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.starttime = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $('#datetimepicker1').datetimepicker({
            format: "yyyy-MM-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.endtime = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $scope.submit_bind = function () {
            if($scope.selection_bind.length==0){
                alert("未选择商超！");
            }else if($scope.starttime==undefined){
                alert("未选择开始时间！");
            }else if($scope.endtime==undefined){
                alert("未选择结束时间！");
            } else{

                prepare_relation($scope.selection_bind, 0);
                function prepare_relation(goods_list, i) {
                    if (i >= goods_list.length) {
                        alert("绑定成功！");
                        window.location.reload();
                        return;
                    }
                    var goods = goods_list[i];
                    $scope.relation={};
                    $scope.relation.aStarttime=$scope.starttime;
                    $scope.relation.aEndtime=$scope.endtime;
                    $scope.relation.aId=$scope.aid;
                    $scope.relation.mId=goods.mId;
                    for(var j=0;j<$scope.relation_outdatelist.length;j++){
                        if($scope.relation_outdatelist[j].mId==$scope.relation.mId){
                            $scope.relation_delete=$scope.relation_outdatelist[j];
                            RelationService.deletebyid($scope.relation_delete.mId,$scope.relation_delete.aId).$promise.then(function(data){
                                RelationService.add($scope.relation,function(data){
                                    prepare_relation(goods_list, i+1);
                                },function(data){
                                    alert("绑定失败！");
                                });
                            })
                        }
                    }
                }
            }
        };

    }]).controller("AdvertiseAddController", ['$scope', '$state', 'AdvService','fileUpload', function ($scope, $state,AdvService, fileUpload) {
        $scope.selected=0;
        $scope.ad={};

        $scope.check=function(){
            $().ready(function() {
                $("#myForm").validate({
                });
            });
        }
        $scope.uploadFile = function(){
            if($scope.myFile==undefined){
                alert("请选择图片！");
                return;
            }
            var file = $scope.myFile;
           fileUpload.uploadFileToUrl(file).then(function(data){
               if(data!=undefined){
                   alert("上传成功！");
                   $scope.ad.aHomepicture=data;
               }
           });
        };
        $scope.uploadFile1 = function(){
            if($scope.myFile1==undefined){
                alert("请选择图片！");
                return;
            }
            var file = $scope.myFile1;
            fileUpload.uploadFileToUrl(file).then(function(data){
                if(data!=undefined){
                    alert("上传成功！");
                    $scope.ad.aDetailpicture=data;
                }
            });
        };

        $scope.submit=function(){
            $scope.ad.aType=$scope.selected;
            AdvService.add($scope.ad,function(data){
                alert("提交成功！");
                //console.log(data);
                $state.go("home1.first.adbind", {id: data.aId});
            },function(data){
                alert("提交失败！")
            })
        }

    }]).controller("AdvertiseAdd1Controller", ['$scope', '$state', '$stateParams','RelationService', 'MarketService', function ($scope, $state, $stateParams,RelationService, MarketService) {

        $scope.markets_outdate=new Array();
        RelationService.getoutdate().$promise.then(function(data){
            $scope.relation_outdatelist=data;
            for(var i=0;i<$scope.relation_outdatelist.length;i++){
                MarketService.getbyid($scope.relation_outdatelist[i].mId).$promise.then(function(data){
                    $scope.markets_outdate.push(data);
                })
            }
        })
        //bind
        $scope.selection_bind=new Array();
        $scope.prepare_data=function(market){
            var  ss=true;
            if($scope.selection_bind.length==0){
                $scope.selection_bind.push(market);
            }else{
                for(var i=0;i<$scope.selection_bind.length;i++){
                    if($scope.selection_bind[i].mId==market.mId){
                        $scope.selection_bind.splice(i,1);
                        ss=false;
                    }
                }
                if(ss){
                    $scope.selection_bind.push(market);
                }
            }
            //console.log($scope.selection_bind);
        }
        $scope.prepare_data1=function(){
            if($scope.master1){
                $scope.selection_bind=[];
                for(var i=0;i<$scope.markets_outdate.length;i++){
                    $scope.selection_bind.push($scope.markets_outdate[i]);
                }
            }else{
                $scope.selection_bind=[];
            }
            //console.log($scope.selection_bind);
        }
        $('#datetimepicker').datetimepicker({
            format: "yyyy-MM-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.starttime = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $('#datetimepicker1').datetimepicker({
            format: "yyyy-MM-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.endtime = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $scope.submit_bind = function () {
            console.log($scope.selection_bind);
            if($scope.selection_bind.length==0){
                alert("未选择商超！");
            }else if($scope.starttime==undefined){
                alert("未选择开始时间！");
            }else if($scope.endtime==undefined){
                alert("未选择结束时间！");
            }
            else{
                var myDate=new Date();
                console.log(myDate);
                if($scope.starttime<myDate){
                    alert("开始时间从当前第二天开始！");
                    return;
                }
                if($scope.starttime<myDate){
                    alert("结束时间请大于当前时间！");
                    return;
                }
                prepare_relation($scope.selection_bind, 0);
                function prepare_relation(goods_list, i) {
                    if (i >= goods_list.length) {
                        alert("绑定成功！");
                        window.location.reload();
                        return;
                    }
                    var goods = goods_list[i];
                    $scope.relation={};
                    $scope.relation.aStarttime=$scope.starttime;
                    $scope.relation.aEndtime=$scope.endtime;
                    $scope.relation.aId=$stateParams.id;
                    $scope.relation.mId=goods.mId;
                    for(var j=0;j<$scope.relation_outdatelist.length;j++){
                        if($scope.relation_outdatelist[j].mId==$scope.relation.mId){
                            $scope.relation_delete=$scope.relation_outdatelist[j];
                            RelationService.deletebyid($scope.relation_delete.mId,$scope.relation_delete.aId).$promise.then(function(data){
                                RelationService.add($scope.relation,function(data){
                                    prepare_relation(goods_list, i+1);
                                },function(data){
                                    alert("绑定失败！");
                                });
                            })
                        }
                    }
                }
            }
        };

        $scope.currentPage = 1;
        $scope.pageSize = 8;

    }])

